<?php

class Kernel {

    private $table;
    private $parentTable;
    private $campos;
    private $aditionalWhereClause;
    private $primaryKeyTable;
    private $totalCampos;

    function __construct() {
       
    }

    public function contarCampos(){
        if(!isset($this->totalCampos)){
            if(!is_array($this->campos)){
                $this->totalCampos = 0;
            }  else {
                $this->totalCampos = count($this->campos);
            }
        }
        
    }

    public function getTable() {
        return $this->table;
    }

    public function setTable($table) {
        $this->table = $table;
    }

    public function getParentTable() {
        return $this->parentTable;
    }

    public function setParentTable($parentTable) {
        $this->parentTable = $parentTable;
    }

    public function getCampos() {
        return $this->campos;
    }

    public function setCampos($campos) {
        $this->campos = $campos;
    }

    public function getAditionalWhereClause() {
        return $this->aditionalWhereClause;
    }

    public function setAditionalWhereClause($aditionalWhereClause) {
        $this->aditionalWhereClause = $aditionalWhereClause;
    }

    public function getPrimaryKeyTable() {
        return $this->primaryKeyTable;
    }

    public function setPrimaryKeyTable($primaryKeyTable) {
        $this->primaryKeyTable = $primaryKeyTable;
    }

    public function getTotalCampos(){
        return $this->totalCampos;
    }

    public function montarListaSQL() {

        $queryBase = "SELECT ";
        $this->contarCampos();
        for ($i = 0; $i < $this->totalCampos; $i++) {
            $CAMPO_NAME = $this->campos[$i][1];
            $CAMPO_TYPE = $this->campos[$i][2];

            if ($CAMPO_TYPE != "MASTER_DETAIL" && $CAMPO_TYPE != "FILES" && $CAMPO_TYPE != "GRID") {
                if ($CAMPO_TYPE == "DATE_TIME" || $CAMPO_NAME == "DT_CADASTRO") {
                    $queryBase .= "DATE_FORMAT(a." . $CAMPO_NAME . ",'%d/%m/%Y') AS $CAMPO_NAME";
                } else {
                    $queryBase .= "$CAMPO_NAME";
                }
                if ($i <  $this->totalCampos - 1) {
                    $queryBase .=",";
                }
            }
        }

        $queryBase .= " FROM $this->table a ";

        if ($this->parentTable) {
            $queryBase .=", $this->parentTable b ";
        }

        $queryBase .= "WHERE 1 = 1";

        if ($this->parentTable) {
            $queryBase .=" AND a.$this->primaryKeyTable = b.$this->primaryKeyTable ";
        }
        if ($this->aditionalWhereClause) {
            $queryBase .= " " . $this->aditionalWhereClause;
        }

        return trim($queryBase);
    }

    public function montarFiltroSQL() {
        extract($_POST);
        $this->contarCampos();
        $queryBase = '';
        for ($i = 0; $i < $this->totalCampos; $i++) {
            $CAMPO_SHOW_FILTER = 0;
            $CAMPO_NAME = $this->campos[$i][1];
            if(isset ($this->campos[$i]['TIPO_PESQUISA'])){
                $TIPO_PESQUISA = $this->campos[$i]['TIPO_PESQUISA'];
            }
            if(isset ($this->campos[$i][9])){
                $CAMPO_SHOW_FILTER = $this->campos[$i][9];
            }
            $CAMPO_TYPE = $this->campos[$i][2];
            if ($CAMPO_SHOW_FILTER == 1 && isset($$CAMPO_NAME)) {
                if($$CAMPO_NAME != ''){
                    if(isset($TIPO_PESQUISA)){
                        if ($TIPO_PESQUISA == 'IGUAL'){
                            $queryBase .= "AND $CAMPO_NAME = '" . $$CAMPO_NAME . "'";
                        }else if($TIPO_PESQUISA == 'LIKE'){
                            $queryBase .= "AND $CAMPO_NAME LIKE '%" . $$CAMPO_NAME . "%' ";
                        }
                    }else{
                        if ($CAMPO_TYPE == "TEXT" || $CAMPO_TYPE == "LONGTEXT") {
                            $queryBase .= "AND $CAMPO_NAME LIKE '%" . $$CAMPO_NAME . "%' ";
                        } else {
                            $queryBase .= "AND $CAMPO_NAME = '" . $$CAMPO_NAME . "'";
                        }
                    }
                }
            }
        }
        return trim($queryBase);
    }

}

?>
